﻿using ISSMoviePlex.DAL.Repositories;
using ISSMoviePlex.DAL.Models;
using System.Collections.Generic;
using System.Linq;

namespace ISSMoviePlex.BLL
{
    public class ScheduleBLL
    {
        private readonly ScheduleRepository _scheduleRepository;
        private readonly RoomRepository _roomRepository;

        public ScheduleBLL()
        {
            _scheduleRepository = ScheduleRepository.Instance;
            _scheduleRepository.Initialize();
            _roomRepository = RoomRepository.Instance;
            _roomRepository.Initialize();
        }

        public bool CheckSeatAvailable(int scheduleId, int numberOfSeats)
        {
            var schedule = _scheduleRepository.FindById(scheduleId);
            return numberOfSeats >= schedule.SeatAvailable;
        }

        public List<Schedule> GetAllSchedules()
        {
            return _scheduleRepository.GetAll().ToList();
        }

        public Schedule GetSchedule(int scheduleId)
        {
            return _scheduleRepository.FindById(scheduleId);
        }

        public List<Schedule> GetScheduleByMovieName(string movieName)
        {
            return _scheduleRepository.GetScheduleByMovieName(movieName);
//            return null;
        }

        public void AddSchedule(Schedule schedule)
        {
            schedule.SeatAvailable = _roomRepository.FindById(schedule.RoomId).Capacity;
            _scheduleRepository.Add(schedule);
            _scheduleRepository.Save();
        }

        public void UpdateSchedule(Schedule schedule)
        {
            _scheduleRepository.Edit(schedule);
            _scheduleRepository.Save();
        }

        public void DeleteSchedule(Schedule schedule)
        {
            _scheduleRepository.Delete(schedule);
            _scheduleRepository.Save();
        }

        public List<Schedule> GetScheduleByMovie(int movieId)
        {
            return _scheduleRepository.FindByMovieId(movieId).ToList();
        }
    }
}
